let app = getApp();
var util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabsIndex: 1,
    time: "",
    pro_id: '',
    type_id: "",
    content: '',
    images: [],
    detail: []
  },

  previewImg2: function (e) {
    let that = this;
    let figureList = that.data.images;
    wx.previewImage({
      current: e.currentTarget.dataset.src,   //当前图片地址
      urls: figureList,        //所有要预览的图片的地址集合 数组形式
    });
  },

  //获取汇报记录
  problemInfo: function () {
    let params = {
      pro_id: this.data.pro_id,
      type_id: this.data.type_id,
    };
    app.request('post', '/api/advance/problemInfo', params).then(res => {
      var detail = res.data.data;
      this.setData({ detail: detail });
      this.setData({ content: detail.content });
      if (detail.end_time != '') {
        this.setData({ time: detail.end_time });
      }
      if(detail.images!=''){
        this.setData({images:detail.images});
      }
    }).catch(err => console.log(err));
  },

  //删除图片
  delImage: function (e) {
    var index = e.currentTarget.dataset.v;
    var images = this.data.images;
    images.splice(index);
    this.setData({ images: images });
  },

  //提交操作
  onSubmit: function () {
    let params = {
      pro_id: this.data.pro_id,
      type_id: this.data.type_id,
      end_time: this.data.time,
      content: this.data.content,
      pic: this.data.images.join(';')
    };
    app.request('post', '/api/advance/createPlan', params).then(res => {
      if (res.data.code == 0) {
        app.showSuccessToast('提交成功');
      }
      this.problemInfo();
    }).catch(err => console.log(err));

  },

  //重置操作
  onRest: function () {
    this.setData({ content: '', images: [] });
  },


  //输入内容
  inputArea1: function (e) {
    var field = e.currentTarget.dataset.field;
    if (field == "content") {
      this.setData({ content: e.detail.value });
    }
  },

  //上传图片
  onChoosePic() {
    let that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        let tempFilePaths = res.tempFilePaths;
        wx.uploadFile({
          url: `${app.globalData.rootUrl}/api/upload/upload`,
          filePath: tempFilePaths[0],
          name: 'file',
          formData: { 'appid': app.appid },
          success: res => {
            let response = JSON.parse(res.data);
            if (res.statusCode == 200) {
              if (response.code == 0) {
                let images = that.data.images;
                images.push(response.thumb);
                that.setData({ images });
              } else {
                app.showFailToast(response.msg);
              }
            } else app.showFailToast('网络错误，请稍后再试');
          }
        });
      }
    })
  },

  //tab切换
  onTab: function (e) {
    this.setData({ tabsIndex: e.currentTarget.dataset.index });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var TIME = util.formatTime(new Date());
    this.setData({
      time: TIME
    });
    if (options.pro_id != undefined) {
      this.setData({ pro_id: options.pro_id });
    }
    if (options.type_id != undefined) {
      this.setData({ type_id: options.type_id });
    }
    this.problemInfo();
  },

  //汇报时间点击
  bindTimeChange:function(e){
      this.setData({ time: e.detail.value });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})